# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
  scipen = 999,
  digits = 16,
  max.print = .Machine$integer.max,
  show.error.locations = TRUE,
  warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Read in all estimates

wage_earnings <-
    readRDS(
        "~/estimation-output/unearned_income_effect_estimates_db_w2_wages_by_marital_status.rds" #nolint
    )
wage_earnings <-
    wage_earnings[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]

# Add omitted
temp <-
    CJ(
        marital_status = unique(wage_earnings$marital_status),
        model = "reduced_form",
        income_quartile = 5
    )
temp[, ref_event_time := -2]
temp[, estimate := 0]
temp[, cluster_se := 0]

wage_earnings <-
    rbindlist(list(wage_earnings, temp), use.names = TRUE)
temp <- NULL
rm(temp)

per_adult_total_empl_income <-
    readRDS(
        "~/estimation-output/unearned_income_effect_estimates_per_adult_total_empl_income_by_marital_status.rds" #nolint
    )
per_adult_total_empl_income <-
    per_adult_total_empl_income[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]

# Add omitted
temp <-
    CJ(
        marital_status = unique(per_adult_total_empl_income$marital_status),
        model = "reduced_form",
        income_quartile = 5
    )
temp[, ref_event_time := -2]
temp[, estimate := 0]
temp[, cluster_se := 0]

per_adult_total_empl_income <-
    rbindlist(list(per_adult_total_empl_income, temp), use.names = TRUE)
temp <- NULL
rm(temp)

# Produce Figure B.9a
# Comparison of estimates of wage earnings change by marital_status

wage_earnings[
    ,
    marital_status :=
    factor(marital_status, levels = c("Married Winner", "Single Winner"))
]

figure_b_9_a <-
    ggplot(
        aes(
            x = ref_event_time,
            y = estimate,
            color = factor(marital_status),
            fill = factor(marital_status)
        ),
        data = wage_earnings
    ) +
    geom_line() +
    geom_ribbon(
        aes(
            ymin = estimate - (1.64 * cluster_se),
            ymax = estimate + (1.64 * cluster_se)
        ),
        linetype = 0,
        alpha = 0.2,
        show.legend = FALSE
    ) +
    theme_bw(base_size = 13) +
    theme(panel.grid.minor = element_blank()) +
    scale_x_continuous(
        breaks = seq.int(from = -7L, to = 5L, by = 1L),
        expand = expansion(mult = c(0.0025, 0.0025))
    ) +
    scale_y_continuous(breaks = seq.int(from = -6000, to = 2000, by = 1000)) +
    labs(x = "Event Time (ℓ)", y = "Event Study Estimate (2016 USD)") +
    theme(
        legend.title = element_blank(),
        legend.position = c(0.13, 0.0825),
        legend.background = element_rect(fill = "white", color = "grey"),
        strip.text.x = element_blank(),
        strip.background = element_blank(),
        legend.key = element_rect(NA),
        legend.spacing.y = unit(2, "mm"),
        legend.key.height = unit(4, "mm"),
        legend.key.width = unit(4, "mm"),
        legend.margin = margin(t = 0, r = 0.1, b = 0.15, l = 0.1, unit = "cm")
    ) +
    scale_color_viridis_d() +
    scale_fill_viridis_d() +
    guides(color = guide_legend(ncol = 1, byrow = TRUE)) +
    coord_cartesian(ylim = c(-6000, 2000))

ggsave(
    plot = figure_b_9_a,
    filename = "~/paper/figures/figure-B.9a.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_9_a,
    filename = "~/paper/figures/figure-B.9a.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
wage_earnings <- NULL
figure_b_9_a <- NULL
rm(wage_earnings, figure_b_9_a)

# Produce Figure B.9b
# Comparison of estimates of per-adult total labor earnings change
# by marital_status

per_adult_total_empl_income[
    ,
    marital_status :=
    factor(marital_status, levels = c("Married Winner", "Single Winner"))
]

figure_b_9_b <-
    ggplot(
        aes(
            x = ref_event_time,
            y = estimate,
            color = factor(marital_status),
            fill = factor(marital_status)
        ),
        data = per_adult_total_empl_income
    ) +
    geom_line() +
    geom_ribbon(
        aes(
            ymin = estimate - (1.64 * cluster_se),
            ymax = estimate + (1.64 * cluster_se)
        ),
        linetype = 0,
        alpha = 0.2,
        show.legend = FALSE
    ) +
    theme_bw(base_size = 13) +
    theme(panel.grid.minor = element_blank()) +
    scale_x_continuous(
        breaks = seq.int(from = -7L, to = 5L, by = 1L),
        expand = expansion(mult = c(0.0025, 0.0025))
    ) +
    scale_y_continuous(breaks = seq.int(from = -6000, to = 2000, by = 1000)) +
    labs(x = "Event Time (ℓ)", y = "Event Study Estimate (2016 USD)") +
    theme(
        legend.title = element_blank(),
        legend.position = c(0.13, 0.0825),
        legend.background = element_rect(fill = "white", color = "grey"),
        strip.text.x = element_blank(),
        strip.background = element_blank(),
        legend.key = element_rect(NA),
        legend.spacing.y = unit(2, "mm"),
        legend.key.height = unit(4, "mm"),
        legend.key.width = unit(4, "mm"),
        legend.margin = margin(t = 0, r = 0.1, b = 0.15, l = 0.1, unit = "cm")
    ) +
    scale_color_viridis_d() +
    scale_fill_viridis_d() +
    guides(color = guide_legend(ncol = 1, byrow = TRUE)) +
    coord_cartesian(ylim = c(-6000, 2000))

ggsave(
    plot = figure_b_9_b,
    filename = "~/paper/figures/figure-B.9b.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_9_b,
    filename = "~/paper/figures/figure-B.9b.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
per_adult_total_empl_income <- NULL
figure_b_9_b <- NULL
rm(per_adult_total_empl_income, figure_b_9_b)